﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web.UI.WebControls;
using Medianamik.Core;
using Medianamik.Core.Sugar;
using Medianamik.Globalization;
using Medianamik.UI.TypeManagement.Controls;

namespace Medianamik.UI.TypeManagement
{
    public partial class PropertyManagement : BaseTypePage
    {
        private Guid? _typeID;
        private Guid? _propertyID;

        protected void Page_Load(object sender, EventArgs e)
        {
            //Un des deux params doit etre présent
            //Un ou l'autre

            string strTypeID = Request.QueryString[DefaultParamNames.TypeID];

            if (!StringHelper.IsGuid(strTypeID, ref _typeID)) // Édition d'une propriété
            {
                string strPropertyID = Request.QueryString["propertyid"];

                if (!StringHelper.IsGuid(strPropertyID, ref _propertyID) || CurrentProperty == null)
                {
                    Response.Redirect(ReturnURL);
                }
            }
            else // Ajout d'une propriété
            {
                if (CurrentType == null)
                {
                    Response.Redirect(ReturnURL);
                }
            }


            if (!IsPostBack)
            {
                LoadDataTypes();
                LoadLookupTypes();

                cblRoles.DataBind();

                if (IsEdition)
                {
                    SetValues();
                }
            }
        }

        protected void chkHaveTooltip_CheckedChanged(object sender, EventArgs e)
        {
            panTooltip.Visible = chkHaveTooltip.Checked;
        }

        //TODO: Valider ça dans les business rules plutôt que dans l'UI
        protected void valPropertyName_ServerValidate(object source, ServerValidateEventArgs args)
        {
            args.IsValid = true;
            Guid id;

            //Si c'est l'édition d'une propriété
            if (_propertyID.HasValue)
            {
                if (CurrentType.PropertyNameExists(txtPropertyName.Text, out id) && !_propertyID.Value.Equals(id))
                    args.IsValid = false;
            }
            else //Nouvelle propriété
            {
                if (CurrentType.PropertyNameExists(txtPropertyName.Text, out id))
                    args.IsValid = false;
            }
        }

        protected void valValidationExpression_ServerValidate(object source, ServerValidateEventArgs args)
        {
            args.IsValid = true;
            if (txtValidationExpression.Text.Length > 0)
            {
                try
                {
                    var regex = new Regex(txtValidationExpression.Text, RegexOptions.CultureInvariant & RegexOptions.IgnoreCase & RegexOptions.Singleline);
                }
                catch (Exception)
                {
                    args.IsValid = false;
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (IsValid)
            {
                var propertyDescription = string.Empty;
                Regex regex = null;
                short? maxLength = null;
                short? minLength = null;
                Guid? lookupPropertyID = null;
                Guid? lookupTypeID = null;
                var required = false;
                var mustBeUnique = false;
                Guid dataTypeId;
                Guid? treeRootNodeId = null;
                var propertyName = txtPropertyName.Text;
                propertyDescription = txtPropertyDescription.Text;
                Guid? medianamikControlID = null;

                if (phrMedianamikControl.Visible && ddlMedianamikControls.SelectedIndex > 0)
                {
                    medianamikControlID = new Guid(ddlMedianamikControls.SelectedValue);
                }

                int? medianamikControlHeigth = null;
                if (phrMedianamikControlHeigth.Visible)
                {
                    medianamikControlHeigth = int.Parse(txtMedianamikControlHeigth.Text);
                }

                Unit? medianamikControlWidth = null;
                if (phrMedianamikControlWidth.Visible)
                {
                    medianamikControlWidth = Unit.Parse(txtMedianamikControlWidth.Text);
                }

                if (phrRequired.Visible)
                {
                    required = chkRequired.Checked;
                }

                if (chkIsLookup.Checked)
                {
                    lookupTypeID = new Guid(ddlLookupTypes.SelectedValue);
                    lookupPropertyID = new Guid(ddlLookupProperties.SelectedValue);
                    treeRootNodeId = RootNodeId;
                    dataTypeId = DataTypes.UniqueIdentifier;
                }
                else
                {
                    if (phrUnique.Visible)
                    {
                        mustBeUnique = chkIsUnique.Checked;
                    }

                    if (phrValidationExpression.Visible)
                    {
                        if (txtValidationExpression.Text.Length > 0)
                        {
                            regex = new Regex(txtValidationExpression.Text, RegexOptions.CultureInvariant & RegexOptions.IgnoreCase & RegexOptions.Singleline);
                        }
                    }

                    if (phrSpecialLength.Visible)
                    {
                        if (txtLength.Text.Length > 0)
                        {
                            maxLength = short.Parse(txtLength.Text);
                        }
                    }
                    else if (phrLength.Visible)
                    {
                        if (txtMaxLength.Text.Length > 0)
                        {
                            maxLength = short.Parse(txtMaxLength.Text);
                        }

                        if (txtMinLength.Text.Length > 0)
                        {
                            minLength = short.Parse(txtMinLength.Text);
                        }
                    }

                    dataTypeId = new Guid(ddlDataTypes.SelectedValue);
                }

                if (IsEdition)
                {
                    CurrentProperty.Name = propertyName;
                    CurrentProperty.Description = propertyDescription;
                    CurrentProperty.HaveTooltip = chkHaveTooltip.Checked;
                    CurrentProperty.IsLanguageNeutral = chkIsLanguageNeutral.Checked;
                    CurrentProperty.IsRequired = required;
                    CurrentProperty.IsUnique = mustBeUnique;
                    CurrentProperty.ValidationRegex = regex;
                    CurrentProperty.GroupName = txtGroupName.Text;
                    CurrentProperty.MedianamikControlID = medianamikControlID;
                    CurrentProperty.MedianamikControlInfo = new MedianamikControlInfo(medianamikControlWidth
                        , medianamikControlHeigth, treeRootNodeId);
                    CurrentProperty.PrimaryTypeID = lookupTypeID;
                    CurrentProperty.PrimaryPropertyID = lookupPropertyID;
                    CurrentProperty.Roles.Clear();
                    CurrentProperty.Roles.AddRange(cblRoles.Items.Cast<ListItem>().Where(i => i.Selected).Select(i => i.Value).ToList());
                    CurrentProperty.MaxLength = maxLength;
                    CurrentProperty.MinLength = minLength;
                    CurrentProperty.Indexable = CurrentDataType.Indexable && chkIndexable.Checked;

                    NodeTypeManager.UpdateProperty(CurrentProperty);

                    Translator.CreateResource("TypeManagement", CurrentProperty.ID + "_Label", txtTranslatorBox.Values);
                    if (chkHaveTooltip.Checked)
                        Translator.CreateResource("TypeManagement", CurrentProperty.ID + "_Tooltip", txtTooltipBox.Values);
                    else
                        Translator.DestroyResource("TypeManagement", CurrentProperty.ID + "_Tooltip", txtTooltipBox.Values);

                    Response.Redirect(ReturnURL);
                }
                else
                {
                    var controlSettings = new MedianamikControlInfo(medianamikControlWidth, medianamikControlHeigth);
                    var prop = CurrentType.AddProperty(propertyName, maxLength, minLength, chkHaveTooltip.Checked, 
                        chkIsLanguageNeutral.Checked, required, mustBeUnique, regex, txtGroupName.Text,
                        medianamikControlID, controlSettings, dataTypeId, lookupTypeID, lookupPropertyID,
                        default(Guid?)/*primaryMedianamikControlID*/, default(MedianamikControlInfo?)/*primaryControlSettings*/, 
                        DataTypes.UniqueIdentifier, treeRootNodeId);
                    prop.Roles.AddRange(cblRoles.Items.Cast<ListItem>().Where(i => i.Selected).Select(i => i.Value).ToList());
                    prop.Description = propertyDescription;
                    prop.Indexable = CurrentDataType.Indexable && chkIndexable.Checked;

                    NodeTypeManager.SaveType(CurrentType);

                    Translator.CreateResource("TypeManagement", prop.ID + "_Label", txtTranslatorBox.Values);
                    
                    if (chkHaveTooltip.Checked)
                        Translator.CreateResource("TypeManagement", prop.ID + "_Tooltip", txtTooltipBox.Values);
                    else
                        Translator.DestroyResource("TypeManagement", prop.ID + "_Tooltip", txtTooltipBox.Values);

                    Response.Redirect(ReturnURL);
                }
            }
        }

        protected void ddlMedianamikControls_SelectedIndexChanged(object sender, EventArgs e)
        {
            //Réinitialiser le contrôle sélectionné
            _currentMedianamikControl = null;

            if (ddlMedianamikControls.SelectedIndex == 0)
            {
                //Cacher tous les champs en rapport au MedianamikControl
                phrMedianamikControlFields.Visible = false;

                phrSpecialLength.Visible = CurrentDataType.MustSpecifyLength;

                //Hack pour ne pas avoir à définir cette valeur 2 fois
                if (CurrentDataType.MustSpecifyLength && txtLength.Text.Length == 0 && txtMaxLength.Text.Length > 0)
                {
                    txtLength.Text = txtMaxLength.Text;
                }

            }
            else
            {
                phrSpecialLength.Visible = false;

                phrMedianamikControlFields.Visible = true;


                if (CurrentMedianamikControl.MaySpecifyWidth)
                {
                    phrMedianamikControlWidth.Visible = true;
                    extMedianamikControlWidth.DefaultValueText = CurrentMedianamikControl.DefaultWidth.ToString();
                }
                else
                {
                    phrMedianamikControlWidth.Visible = false;
                }



                if (CurrentMedianamikControl.MaySpecifyHeight)
                {
                    phrMedianamikControlHeigth.Visible = true;
                    extMedianamikControlHeigth.DefaultValueText = CurrentMedianamikControl.DefaultHeight.ToString();
                }
                else
                {
                    phrMedianamikControlHeigth.Visible = false;
                }



                if (CurrentMedianamikControl.MaySpecifyLength)
                {
                    phrLength.Visible = true;

                    //Hack pour ne pas avoir à définir cette valeur 2 fois
                    if (txtLength.Text.Length > 0 && txtMaxLength.Text.Length == 0)
                    {
                        txtMaxLength.Text = txtLength.Text;
                    }
                }
                else
                {
                    phrLength.Visible = false;
                }



                phrUnique.Visible = (CurrentMedianamikControl.MayBeUnique);



                phrValidationExpression.Visible = (CurrentMedianamikControl.MaySpecifyValidationExpression);



                phrRequired.Visible = (CurrentMedianamikControl.MayBeRequired);

            }
        }

        protected void ddlDataTypes_SelectedIndexChanged(object sender, EventArgs e)
        {
            _currentDataType = null;
            LoadMedianamikControls(chkIsLookup.Checked ? MedianamikControlType.LOOKUP : MedianamikControlType.DEFAULT);
        }

        protected void ddlLookupTypes_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlLookupTypes.SelectedIndex == 0)
            {
                phrLookupFields.Visible = false;
            }
            else
            {
                phrLookupFields.Visible = true;
                LoadLookupProperties();
            }
        }

        protected void chkIsLookup_CheckedChanged(object sender, EventArgs e)
        {
            //TODO : Logique pour ne pas avoir à resélectionner
            //       les valeurs si elle avaient déjà été spécifiées

            if (chkIsLookup.Checked)
            {
                phrLookupType.Visible = true;
                phrDataTypeFields.Visible = false;
                phrIndexation.Visible = false;
                phrLanguageNeutral.Visible = false;
                phrMedianamikControl.Visible = true;
            }
            else
            {
                phrLookupType.Visible = false;
                phrDataTypeFields.Visible = true;
                phrIndexation.Visible = true;
                phrLanguageNeutral.Visible = true;
                phrMedianamikControl.Visible = ddlDataTypes.SelectedIndex > 0;
            }

            LoadMedianamikControls(chkIsLookup.Checked ? MedianamikControlType.LOOKUP : MedianamikControlType.DEFAULT);
        }

        private NodeTypeProperty _currentProperty;
        public NodeTypeProperty CurrentProperty
        {
            get
            {
                if (_currentProperty == null && _propertyID.HasValue)
                {
                    _currentProperty = NodeTypeManager.GetTypeProperty(_propertyID.Value);
                }

                return _currentProperty;
            }
        }

        private NodeType _currentType;
        public NodeType CurrentType
        {
            get
            {
                if (_currentType == null)
                {
                    _currentType = _typeID.HasValue
                                 ? NodeTypeManager.GetNodeType(_typeID.Value)
                                 : NodeTypeManager.GetNodeType(CurrentProperty.TypeID);
                }

                return _currentType;
            }
        }

        private DataType _currentDataType;
        public DataType CurrentDataType
        {
            get
            {
                if (_currentDataType == null)
                {
                    if (chkIsLookup.Checked)
                    {
                        _currentDataType = ConfigManager.GetDataTypeById(DataTypes.UniqueIdentifier);
                    }
                    else if (ddlDataTypes.SelectedIndex > 0)
                    {
                        _currentDataType = ConfigManager.GetDataTypeById(new Guid(ddlDataTypes.SelectedValue));
                    }
                }

                return _currentDataType;
            }
        }

        private MedianamikControl _currentMedianamikControl;
        public MedianamikControl CurrentMedianamikControl
        {
            get
            {
                if (_currentMedianamikControl == null && ddlMedianamikControls.SelectedIndex > 0)
                {
                    _currentMedianamikControl = ConfigManager.GetMedianamikControl(new Guid(ddlMedianamikControls.SelectedValue));
                }

                return _currentMedianamikControl;
            }
        }

        private NodeType _currentPrimaryType;
        public NodeType CurrentLookupType
        {
            get
            {
                if (_currentPrimaryType == null && ddlLookupTypes.SelectedIndex > 0)
                {
                    _currentPrimaryType = NodeTypeManager.GetNodeType(new Guid(ddlLookupTypes.SelectedValue));
                }

                return _currentPrimaryType;
            }
        }

        private string _returnURL;
        public string ReturnURL
        {
            get
            {
                if (_returnURL == null)
                {
                    if (CurrentType == null)
                    {
                        _returnURL = "TypeList.aspx";
                    }
                    else
                    {
                        _returnURL = "propertylist.aspx?" + Medianamik.Core.DefaultParamNames.TypeID + "=" + CurrentType.ID;
                        if (CameFromSearchPage)
                        {
                            _returnURL = GetSearchURL(_returnURL);
                        }
                    }
                }

                return _returnURL;
            }
        }

        private bool IsEdition
        {
            get
            {
                return _propertyID.HasValue;
            }
        }

        public Guid? RootNodeId
        {
            get
            {
                Guid g;
                if (StringHelper.IsGuid(RootNodeIdComboBox.SelectedValue, out g))
                {
                    return g;
                }
                return null;
            }
        }

        /// <summary>
        /// Affecte les valeurs aux champs de la page selon le NodeTypeProperty à éditer
        /// </summary>
        private void SetValues()
        {
            //Désactiver certaines options si le type ou l'un de ses enfants a des noeuds
            //chkIsLookup.Enabled = ddlMedianamikControls.Enabled = ddlDataTypes.Enabled = chkIsLanguageNeutral.Enabled = chkIsUnique.Enabled = chkRequired.Enabled = !CurrentType.HasNodes;

            chkIsLookup.Enabled = false;
            ddlDataTypes.Enabled = false;

            //Cette vérification doit être fait avant LoadMedianamikControls
            chkIsLookup.Checked = CurrentProperty.IsLookup;
            chkHaveTooltip.Checked = CurrentProperty.HaveTooltip;
            panTooltip.Visible = CurrentProperty.HaveTooltip;


            if (chkIsLookup.Checked) //Propriété de type lookup
            {
                phrLookupType.Visible = true;
                phrDataTypeFields.Visible = false;

                phrLanguageNeutral.Visible = false;


                phrMedianamikControl.Visible = true;

                LoadMedianamikControls(MedianamikControlType.LOOKUP);

                ddlMedianamikControls.SelectedValue = CurrentProperty.MedianamikControlID.ToString();

                if (ddlMedianamikControls.SelectedIndex > 0)
                {
                    phrMedianamikControlFields.Visible = true;

                    phrLength.Visible = false;
                    phrSpecialLength.Visible = false;


                    phrMedianamikControlWidth.Visible = CurrentMedianamikControl.MaySpecifyWidth;

                    if (phrMedianamikControlWidth.Visible)
                    {
                        txtMedianamikControlWidth.Text = CurrentProperty.MedianamikControlInfo.Width.ToString();
                        extMedianamikControlWidth.DefaultValueText = CurrentMedianamikControl.DefaultWidth.ToString();
                    }



                    phrMedianamikControlHeigth.Visible = CurrentMedianamikControl.MaySpecifyHeight;

                    if (phrMedianamikControlHeigth.Visible)
                    {
                        txtMedianamikControlHeigth.Text = CurrentProperty.MedianamikControlInfo.Height.ToString();
                        extMedianamikControlHeigth.DefaultValueText = CurrentMedianamikControl.DefaultHeight.ToString();
                    }



                    if (CurrentProperty.MedianamikControlInfo.TreeRootNodeID.HasValue)
                    {
                        RootNodeIdComboBox.SetSelectedNode(CurrentProperty.MedianamikControlInfo.TreeRootNodeID.Value);

                    }

                }
                else
                {
                    phrMedianamikControlFields.Visible = false;
                }



                ddlLookupTypes.SelectedValue = CurrentProperty.PrimaryTypeID.ToString();


                if (ddlLookupTypes.SelectedIndex > 0)
                {
                    phrLookupFields.Visible = true;


                    LoadLookupProperties();

                    ddlLookupProperties.SelectedValue = CurrentProperty.PrimaryPropertyID.ToString();

                }
                else
                {
                    phrLookupFields.Visible = false;
                }
            }
            else //Propriété régulière
            {
                phrLookupType.Visible = false;
                phrDataTypeFields.Visible = true;
                phrLanguageNeutral.Visible = true;


                //TODO : Exposer DataTypeId sur l'objet NodeTypeProperty pour éviter d'avoir a charger le DataType complet
                ddlDataTypes.SelectedValue = CurrentProperty.DataType.ID.ToString();

                if (ddlDataTypes.SelectedIndex > 0)
                {
                    phrMedianamikControl.Visible = true;

                    LoadMedianamikControls(MedianamikControlType.DEFAULT);

                    ddlMedianamikControls.SelectedValue = CurrentProperty.MedianamikControlID.ToString();

                    if (ddlMedianamikControls.SelectedIndex > 0)
                    {
                        phrMedianamikControlFields.Visible = true;

                        phrSpecialLength.Visible = false;


                        phrMedianamikControlWidth.Visible = CurrentMedianamikControl.MaySpecifyWidth;

                        if (phrMedianamikControlWidth.Visible)
                        {
                            txtMedianamikControlWidth.Text = CurrentProperty.MedianamikControlInfo.Width.ToString();
                            extMedianamikControlWidth.DefaultValueText = CurrentMedianamikControl.DefaultWidth.ToString();
                        }



                        phrMedianamikControlHeigth.Visible = CurrentMedianamikControl.MaySpecifyHeight;

                        if (phrMedianamikControlHeigth.Visible)
                        {
                            txtMedianamikControlHeigth.Text = CurrentProperty.MedianamikControlInfo.Height.ToString();
                            extMedianamikControlHeigth.DefaultValueText = CurrentMedianamikControl.DefaultHeight.ToString();
                        }


                        phrLength.Visible = CurrentMedianamikControl.MaySpecifyLength;

                        if (phrLength.Visible)
                        {
                            txtMaxLength.Text = CurrentProperty.MaxLength.HasValue ? CurrentProperty.MaxLength.Value.ToString() : String.Empty;
                            txtMinLength.Text = CurrentProperty.MinLength.HasValue ? CurrentProperty.MinLength.Value.ToString() : String.Empty;
                        }
                    }
                    else
                    {
                        phrMedianamikControlFields.Visible = false;
                    }

                    if (CurrentDataType.MustSpecifyLength && !phrLength.Visible)
                    {
                        phrSpecialLength.Visible = true;
                        txtLength.Text = CurrentProperty.MaxLength.HasValue ? CurrentProperty.MaxLength.Value.ToString() : String.Empty;
                    }
                    else
                    {
                        phrSpecialLength.Visible = false;
                    }
                    if (phrIndexation.Visible && CurrentDataType.Indexable)
                    {
                        chkIndexable.Checked = CurrentProperty.Indexable;
                    }
                    else
                    {
                        chkIndexable.Checked = false;
                    }
                }
                else
                {
                    phrMedianamikControl.Visible = false;
                }



                if (CurrentProperty.MedianamikControl != null && CurrentProperty.MedianamikControl.MayBeUnique)
                {
                    phrUnique.Visible = true;
                    chkIsUnique.Checked = CurrentProperty.IsUnique;

                    if (!chkIsUnique.Checked)
                        chkIsUnique.Enabled = true; // Ne fonctionne pas => !NodeTypeManager.PropertyHasDuplicateValues(CurrentProperty.ID);
                }
                else
                {
                    phrUnique.Visible = false;
                }



                if (CurrentProperty.MedianamikControl != null && CurrentProperty.MedianamikControl.MaySpecifyValidationExpression)
                {
                    phrValidationExpression.Visible = true;
                    txtValidationExpression.Text = CurrentProperty.ValidationRegex == null ? "" : CurrentProperty.ValidationRegex.ToString();
                }
                else
                {
                    phrValidationExpression.Visible = false;
                }

            }



            if (CurrentProperty.MedianamikControl != null && CurrentProperty.MedianamikControl.MayBeRequired)
            {
                phrRequired.Visible = true;
                chkRequired.Checked = CurrentProperty.IsRequired;
            }
            else
            {
                phrRequired.Visible = false;
            }




            chkIsLanguageNeutral.Checked = CurrentProperty.IsLanguageNeutral;




            txtPropertyName.Text = CurrentProperty.Name;
            txtPropertyDescription.Text = CurrentProperty.Description;



            txtGroupName.Text = CurrentProperty.GroupName;



            cblRoles.SelectedRoles = CurrentProperty.Roles;



            txtTranslatorBox.Values = Translator.GetResources("TypeManagement",
                CurrentProperty.ID + "_Label",
                ConfigManager.GetMedianamikCultures().Select(c => c.CultureInfo));
            txtTooltipBox.Values = Translator.GetResources("TypeManagement",
                CurrentProperty.ID + "_Tooltip",
                ConfigManager.GetMedianamikCultures().Select(c => c.CultureInfo));

        }

        private void LoadLookupTypes()
        {
            if (NodeTypeManager.EligibleLookupTypes.Count() > 0)
            {
                ddlLookupTypes.DataSource = NodeTypeManager.EligibleLookupTypes;
                ddlLookupTypes.DataBind();
                ddlLookupTypes.Items.Insert(0, "");
            }
            else
            {
                chkIsLookup.Visible = false;
            }
        }

        private void LoadMedianamikControls(MedianamikControlType pControlType)
        {
            if (pControlType != MedianamikControlType.DEFAULT
                && pControlType != MedianamikControlType.LOOKUP)
            {
                throw new ArgumentException("pControlType must be DEFAULT or LOOKUP");
            }


            phrMedianamikControlFields.Visible = false;

            var controls = new Collection<MedianamikControl>();
            if (pControlType == MedianamikControlType.LOOKUP)
            {
                controls = ConfigManager.GetMedianamikControls(DataTypes.UniqueIdentifier,
                                    MedianamikControlType.LOOKUP);
                phrSpecialLength.Visible = false;
                phrIndexation.Visible = false;
            }
            else if (ddlDataTypes.SelectedIndex < 1)
            {
                phrSpecialLength.Visible = false;
                phrIndexation.Visible = false;
            }
            else
            {
                controls = ConfigManager.GetMedianamikControls(new Guid(ddlDataTypes.SelectedValue),
                   pControlType);
                phrSpecialLength.Visible = CurrentDataType.MustSpecifyLength;
                phrIndexation.Visible = CurrentDataType.Indexable;
            }

            if (controls.Count > 0)
            {
                phrMedianamikControl.Visible = true;
                ddlMedianamikControls.DataSource = controls;
                ddlMedianamikControls.DataBind();
                ddlMedianamikControls.Items.Insert(0, "");
            }
            else
            {
                phrMedianamikControl.Visible = false;
            }
        }

        private void LoadLookupProperties()
        {
            ddlLookupProperties.DataSource = CurrentLookupType.GetAllProperties()
                .Where(p => p.DataType.ID == DataTypes.Varchar || p.DataType.ID == DataTypes.Int); /*On va chercher les props héritées également*/
            ddlLookupProperties.DataBind();
            ddlLookupProperties.Items.Insert(0, "");
        }

        private void LoadDataTypes()
        {
            ddlDataTypes.DataSource = ConfigManager.GetAllDataTypes();
            ddlDataTypes.DataBind();
            ddlDataTypes.Items.Insert(0, "");
        }
    }
}